
package sg.edu.nus.iss.cats.model;

import java.util.*;
import sg.edu.nus.iss.cats.controller.UserManager;

/**
 * User class
 *
 * @version $Revision: 1.0
 */

public class User implements java.io.Serializable {
	
    private String  userId;
    private String  name;
    private String  password;
    private String  employeeId;

    private ArrayList roleIds = new ArrayList ();
   
    public User () {
    
    }
    
    public User(String userId,String name,String password,String employeeId){
    this.setUserId (userId);
      this.name = name;
       this.password = password;
       this.employeeId = employeeId;
        
    
    }

    public User (String userId) {
        this.setUserId (userId);
    }

    public void setUserId (String userId) {
        this.userId = userId;
    }

    public String getUserId (){
        return userId;
    }

    public void setName (String name) {
        this.name = name;
    }

    public String getName (){
        return name;
    }

    public void setPassword (String password) {
        this.password = password;
    }

    public String getPassword (){
        return password;
    }

    public void setEmployeeId (String employeeId) {
        this.employeeId = employeeId;
    }

    public String getEmployeeId () {
        return employeeId;
    }

    public void setRoleIds (String[] newRoles) {
        //clearRoleIds ();
        for (int i = 0; i < newRoles.length; i++) {
            addRoleId (newRoles[i]);
        }
    }

    public String[] getRoleIds () {
        int count = roleIds.size();
        String[] result = new String [count];
        result = (String[]) roleIds.toArray (result);
        return result;
    }
    
    
    public void setRoles (Role[] newRoles) {
        clearRoleIds ();
        for (int i = 0; i < newRoles.length; i++) {
            addRoleId (newRoles[i].getRoleId());
        }
    }

    public Role[] getRoles () {
        System.out.println("USER: Get roles: " + roleIds.size());
        int count = roleIds.size();
        Role[] result = new Role [count];      
        Vector v = new Vector();
        for (int i = 0; i < count; i++) {
            try {
                UserManager userMgr = new UserManager();
                String s = (String)roleIds.get(i);
                System.out.println("USER: Get role info for : " + s);
                Role newRole = userMgr.findRole(s);
                
                System.out.println("USER: ROLENAME : " + newRole.getName());
                v.add (newRole);
            } catch (Exception e) {
                System.out.println ("Error getting list of role ids: "+e);
            }
            
        }
        result = (Role[]) v.toArray (result);
        System.out.println("USER: returning : " + result.length);
        return result;
        
    }
    
    public boolean hasRole (String roleId) {
        if (roleId == null) {
           return false;
        }
        Iterator i = roleIds.iterator ();
        while (i.hasNext()) {
            String r = (String) i.next();
            if (roleId.equals(r)) {
                return true;
            }
        }
        return false;
    }

    public void clearRoleIds () {
        roleIds = new ArrayList ();
    }

    public void addRoleId (String newRole) {
        roleIds.add (newRole);
    }
    
    public void addRole (Role newRole) {
            roleIds.add (newRole.getRoleId());
    }

	

}